package com.estun.backroundManagementService.modules.position.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.estun.backroundManagementService.modules.position.dto.PositionOptionTreeResult;
import com.estun.backroundManagementService.modules.position.dto.PositionParam;
import com.estun.backroundManagementService.modules.position.mapper.FeedingPortMapper;
import com.estun.backroundManagementService.modules.position.mapper.PositionMapper;
import com.estun.backroundManagementService.modules.position.model.FeedingPort;
import com.estun.backroundManagementService.modules.position.model.Position;
import com.estun.backroundManagementService.modules.position.service.FeedingPortService;
import com.estun.backroundManagementService.modules.position.service.PositionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 工位表 服务实现类
 * </p>
 *
 * @author machine
 * @since 2023-02-02
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {


    @Autowired
    private FeedingPortMapper feedingPortMapper;


    @Override
    public boolean delete(Integer id) {
        return removeById(id);
    }

    @Override
    public List<PositionOptionTreeResult> listTree() {
        List<Position> positionList = list();
        List<FeedingPort> feedingPortList = feedingPortMapper.selectList(Wrappers.emptyWrapper());
        Map<String, List<FeedingPort>> feedingPortMap = feedingPortList.stream()
                .collect(Collectors.groupingBy(FeedingPort::getStationId)); // 根据 stationId 对 FeedingPort 进行分组

        return positionList.stream().map(position -> {
            PositionOptionTreeResult result = new PositionOptionTreeResult();
            result.setLabel(position.getName());
            result.setValue(position.getId().toString());

            List<PositionOptionTreeResult> children = Optional.ofNullable(feedingPortMap.get(position.getStationId()))
                    .orElse(Collections.emptyList())
                    .stream()
                    .map(feedingPort -> {
                        PositionOptionTreeResult child = new PositionOptionTreeResult();
                        child.setLabel(feedingPort.getName());
                        child.setValue(feedingPort.getId().toString());
                        return child;
                    })
                    .collect(Collectors.toList());

            result.setChildren(children);
            return result;
        }).collect(Collectors.toList());
    }

    @Override
    public Position getByStationId(String stationId) {
        return getOne(new QueryWrapper<Position>().eq("station_id", stationId));
    }

}
